package com.handpay.zztong.hp.login.activity;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.login.model.modifyloginpwd.ModifyLoginPWDModel;
import com.handpay.zztong.hp.login.model.sendphonecode.SendAuthCodeModel;
import com.handpay.zztong.hp.ui.VerificationCodeButton;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;


/**
 * 修改登陆密码
 */
public class ModifyLoginPwdActivity extends ZZTongActivity {

    private EditText input_old_password;
    private EditText input_new_password;
    private EditText sure_new_password;
    private EditText messagecode;
    private VerificationCodeButton getmassagecode;
    private boolean isforceUpdate;//是否牵制修改密码

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Update_Password), true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isforceUpdate) {
                    goBack();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_modify_login_pwd);
        super.onCreate(savedInstanceState);
        isforceUpdate = getIntent().getBooleanExtra("forceUpdate", false);
        input_old_password = (EditText) findViewById(R.id.input_old_password);
        input_new_password = (EditText) findViewById(R.id.input_new_password);
        sure_new_password = (EditText) findViewById(R.id.sure_new_password);
        messagecode = (EditText) findViewById(R.id.messagecode);
        getmassagecode = (VerificationCodeButton) findViewById(R.id.getmassagecode);
        Button confirm = (Button) findViewById(R.id.btn_confirm);
        getmassagecode.setOnClickListener(this);
        confirm.setOnClickListener(this);
        Initialization();
//        authCodeBill = new AuthCodeBill(this);
    }


    protected boolean isShownTabs() {
        return false;
    }

    /***
     * 初始化基本信息
     */
    private void Initialization() {

    }

    /***
     * 获得验证码
     */
    private void DozztSendPhoneCode() {
        if (!CommonUtils.isMobileNum(AccountUtils.getAccountPhone(this))) {
            //		this.showAlertDialog(this, R.string.tip,R.string.input_right_phone_tip);
            Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
            return;
        }
        new SendAuthCodeModel().doSendAuthCodePost(this, ZZTConstant.AUTHCODE_ACTION_MODIPWD, AccountUtils.getAccountPhone(this), new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                if (bean.getResponseCode().equals("0")) {
                    Toast.makeText(ModifyLoginPwdActivity.this, "发送验证码成功！", Toast.LENGTH_SHORT).show();
                } else {
                    showAlertDialog(ModifyLoginPwdActivity.this, getString(R.string.prompt), bean.getErrMessage());
                }
            }
        });
        getmassagecode.startLocker(-1);
    }

    /***
     * 修改/重置账户密码
     */
    private void DoModifyResetPassword() {
        Hashtable<String, String> params = new Hashtable<>();
        params.put("account", SecureManager.getInstance().des(AccountUtils.getAccountPhone(this), 1, null));
        params.put("oldPwd", SecureManager.getInstance().des(input_old_password.getText().toString().trim(), 1, null));
        params.put("newPwd", SecureManager.getInstance().des(input_new_password.getText().toString().trim(), 1, null));
        params.put("action", ZZTConstant.AUTHCODE_ACTION_MODIPWD);
        params.put("verifyCode", messagecode.getText().toString().trim());
        params.put("channel", ClientEngine.CHANNEL);
//        doJsonPost(this, Do_ModifyResetPassword, ResponseBean.class, params);
        new ModifyLoginPWDModel().doModifyResetPasswordPost(this, params, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                showAlertDialog(ModifyLoginPwdActivity.this, getString(R.string.tip), getString(R.string.update_password_tip_Success), false, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(ModifyLoginPwdActivity.this, LoginActivity.class);
                        ModifyLoginPwdActivity.this.startActivity(intent);
                        ModifyLoginPwdActivity.this.finish();
                        dialog.dismiss();
                    }
                });
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private boolean IsControlValues() {
        if (input_old_password.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.alert_input_oldpdw, Toast.LENGTH_SHORT).show();
            return false;
        } else if (input_new_password.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.alert_input_newpdw, Toast.LENGTH_SHORT).show();
            return false;
        } else if (!AccountUtils.isPWDLegal(input_new_password.getText().toString().trim(), AccountUtils.PWD_MIN_LEN)) {
            Toast.makeText(this, R.string.illegal_input_password, Toast.LENGTH_SHORT).show();
            return false;
        } else if (!input_new_password.getText().toString().trim().equals(sure_new_password.getText().toString().trim())) {
            Toast.makeText(this, R.string.input_error_reset_zzt_pwd_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (messagecode.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.input_right_message_code_tip, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.getmassagecode) {// 获取验证码
            DozztSendPhoneCode();
        } else if (i == R.id.btn_confirm) {// 确认修改手机号
            if (IsControlValues()) {
                DoModifyResetPassword();
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isforceUpdate) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
